package com.lsj.gulimall.member.controller;

import com.lsj.common.exception.BizCodeEnum;
import com.lsj.common.utils.PageUtils;
import com.lsj.common.utils.R;
import com.lsj.gulimall.member.entity.MemberEntity;
import com.lsj.gulimall.member.execption.PhoneExistException;
import com.lsj.gulimall.member.execption.UserNameExistException;
import com.lsj.gulimall.member.service.MemberService;
import com.lsj.gulimall.member.vo.GiteeSocialUser;
import com.lsj.gulimall.member.vo.MemberRegisterVo;
import com.lsj.gulimall.member.vo.UserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 会员
 *
 * @author lsj
 * @email 1326646663@qq.com
 * @date 2022-01-06 16:30:17
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVo vo){
        try {
            memberService.register(vo);
        } catch (PhoneExistException e) {
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
        } catch (UserNameExistException e) {
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }


    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo vo){
        MemberEntity memberEntity = memberService.login(vo);
        if (memberEntity != null) {
            return R.ok().put("member",memberEntity);
        }
        return R.error(BizCodeEnum.USER_PHONE_NOTMATCH_EXCEPTION.getCode(), BizCodeEnum.USER_PHONE_NOTMATCH_EXCEPTION.getMsg());
    }


    @PostMapping("/socialLogin")
    public R socialLogin(@RequestBody GiteeSocialUser socialUser) throws Exception {
        MemberEntity memberEntity = memberService.socialLogin(socialUser);
        if (memberEntity != null) {
            return R.ok().put("data",memberEntity);
        }
        return R.error(BizCodeEnum.SOCIAL_USER_LOGIN_EXCEPTION.getCode(), BizCodeEnum.SOCIAL_USER_LOGIN_EXCEPTION.getMsg());
    }

}
